import { getInnspectionitemsAll } from '@/api/q1/basedata/innspectionItems'
import { getInnspectionEntryAll } from '@/api/q1/basedata/innspectionEntry'
import { GetInnspectionMethodAll } from '@/api/q1/basedata/innspectionmethod'
import { GetSupplierAll } from '@/api/q1/basedata/supplier'
import { getCustomerAll } from '@/api/q1/basedata/customer'
import { getDefactAll } from '@/api/q1/basedata/defect'
import { getSamplingPlanAll } from '@/api/q1/basedata/samplingPlan'
const state = {
    innspectionItemsList: [],
    innspectionEntryList: [],
    innspectionmethodList: [],
    supplierList: [],
    customerList: [],
    defectList:[],
    aqlList:[],
}

const mutations = {
    SET_INNSPECTIONITEMS_LIST: (state, innspectionItemsList) => {
        state.innspectionItemsList = innspectionItemsList
    },
    SET_INNSPECTIONENTRY_LIST: (state, innspectionEntryList) => {
        state.innspectionEntryList = innspectionEntryList
    },
    SET_INNSPECTIONMETHOD_LIST: (state, innspectionmethodList) => {
        state.innspectionmethodList = innspectionmethodList
    },
    SET_SUPPLIER_LIST:(state, supplierList) => {
        state.supplierList = supplierList
    },
    SET_CUSTOMER_LIST:(state, customerList) => {
        state.customerList = customerList
    },
    SET_DEFECT_LIST:(state, defectList) => {
        state.defectList = defectList
    },
    SET_AQL_LIST:(state, aqlList) => {
        state.aqlList = aqlList
    },
}

const actions = {
    getInnspectionCustomerData({commit}) {
        return new Promise((resolve, reject) => {
            getCustomerAll().then(res => {
                commit('SET_CUSTOMER_LIST', res.data.list)
                resolve(res.data.list)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getInnspectionSupplierData({commit}) {
        return new Promise((resolve, reject) => {
            GetSupplierAll().then(res => {
                commit('SET_SUPPLIER_LIST', res.data.list)
                resolve(res.data.list)
            }).catch(error => {
                reject(error)
            })
        })
    },

    getInnspectionItemsAll({ commit }) {
        return new Promise((resolve, reject) => {
            getInnspectionitemsAll().then(res => {
                commit('SET_INNSPECTIONITEMS_LIST', res.data.list)
                resolve(res.data.list)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getInnspectionItemsData({ state, dispatch }) {
        return new Promise(async resolve => {
            let list = []
            if (!state.innspectionItemsList.length) {
                list = await dispatch('getInnspectionItemsAll')
            } else {
                list = state.innspectionItemsList
            }
            resolve(list)
        })
    },
    getInnspectionEntryAll({ commit }) {
        return new Promise((resolve, reject) => {
            getInnspectionEntryAll().then(res => {
                commit('SET_INNSPECTIONENTRY_LIST', res.data.list)
                resolve(res.data.list)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getInnspectionEntryData({ state, dispatch }) {
        return new Promise(async resolve => {
            let list = []
            if (!state.innspectionEntryList.length) {
                list = await dispatch('getInnspectionEntryAll')
            } else {
                list = state.innspectionEntryList
            }
            resolve(list)
        })
    },
    getInnspectionMethodAll({ commit }) {
        return new Promise((resolve, reject) => {
            GetInnspectionMethodAll().then(res => {
                commit('SET_INNSPECTIONMETHOD_LIST', res.data.list)
                resolve(res.data.list)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getInnspectionMethodData({ state, dispatch }) {
        return new Promise(async resolve => {
            let list = []
            if (!state.innspectionmethodList.length) {
                list = await dispatch('getInnspectionMethodAll')
            } else {
                list = state.innspectionmethodList
            }
            resolve(list)
        })
    },
    getDefactAll({ commit }) {
        return new Promise((resolve, reject) => {
            getDefactAll().then(res => {
                commit('SET_DEFECT_LIST', res.data.list)
                resolve(res.data.list)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getDefactData({ state, dispatch }) {
        return new Promise(async resolve => {
            let list = []
            if (!state.defectList.length) {
                list = await dispatch('getDefactAll')
            } else {
                list = state.defectList
            }
            resolve(list)
        })
    },
    getAQLAll({ commit }) {
        return new Promise((resolve, reject) => {
            getSamplingPlanAll().then(res => {
                commit('SET_AQL_LIST', res.data)
                resolve(res.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getAQLData({ state, dispatch }) {
        return new Promise(async resolve => {
            let list = []
            if (!state.aqlList.length) {
                list = await dispatch('getAQLAll')
            } else {
                list = state.aqlList
            }
            resolve(list)
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}